package main

import (
	"flag"
	"fmt"
	"net/http"

	"gitee.com/segeln/grit/log"

	"gitee.com/segeln/grit"
	"gitee.com/segeln/grit/conf"
	"gitee.com/segeln/grit/gatehttp"
	"github.com/gorilla/mux"
)

var (
	gateservice *gatehttp.GateHttp
)

func hander1(writer http.ResponseWriter, request *http.Request) {
	vars := mux.Vars(request)
	fmt.Println("id:", vars["id"])
	fmt.Println("hander1")
	fmt.Println("ddd:", request.FormValue("ddd"))
}

func hander2(writer http.ResponseWriter, request *http.Request) {
	fmt.Println("hander2")
}
func usage() {
	flag.PrintDefaults()
	//	os.Exit(2)
}

func init() {
	flag.Usage = usage
	//flag.Set("logtostderr", "true")
	//flag.Set("stderrthreshold", "WARNING")
	//flag.Set("v", "2")
	// This is wa
	flag.Parse()
}

func BeforeAction(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.String() == `/index/ad?ddd=123` {
			return
		}
		h.ServeHTTP(w, r)
	})
}

func initRoute() { //gate *gatehttp.GateHttp) {

	gateservice.AddRoute(gatehttp.Route{[]string{"GET"}, "/index/{id}", hander1, ""})
	gateservice.AddRoute(gatehttp.Route{[]string{"GET"}, "/index2", hander2, ""})
}
func stop() {
	log.Errorln("stop..............")
}
func main() {
	//flag.Set("v", "2")
	//flag.Set("stderrthreshold", "WARNING")
	//flag.Parse()
	cfg := flag.String("cfg", "server.cfg", "service config file")
	flag.Parse()
	conf.SetConfigFile(*cfg)
	defer stop()
	log.Errorln("stop.222.............")
	gateservice = grit.NewGateHttp(
		conf.Staticdir([]string{"public", "static"}),
	)
	initRoute()
	gateservice.FrontAction = BeforeAction
	gateservice.Init()
	gateservice.Run()
}
